package cz.muni.fi.pa165.sportclubmanager.dao;

import cz.muni.fi.pa165.sportclubmanager.entity.Player;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


/**
 *
 * @author Matus Duchon (xduchon2@fi.muni.cz)
 */
@Stateless
public class PlayerDAOImpl implements PlayerDAO {

    @PersistenceContext
    private EntityManager em;
    
    /**
     * Stores entity Player into persistence. Player has to be valid (see validate())
     * and it should not have defined Id, otherwise an IllegalArgumentException is raised.
     *
     * @param p Player entity instance, without previously defined Id
     * @throws IllegalArgumentException
     */
    @Override
    public void create(Player p) {
        if (!validate(p) || p.getId() != null) {
            throw new IllegalArgumentException();
        }
        em.persist(p);
    }



    /**
     * Updates record of entity Player in persistence with given instance, based on their Ids.
     * Given updating instance should be valid (see validate()) and it has to have its Id defined,
     * otherwise an IllegalArgumentException is raised.
     *
     * @param p Player entity instance
     * @throws IllegalArgumentException
     */
    @Override
    public void update(Player p) {
        if (!validate(p) || p.getId()==null) {
            throw new IllegalArgumentException();
        }
        em.merge(p);
    }



    /**
     * Remove record of given entity Player instance from persistence.
     * Instance has to have its Id defined, otherwise an IllegalArgumentException is raised
     *
     * @param p Player entity instance
     * @throws IllegalArgumentException
     */
    @Override
    public void remove(Player p) {
        if (p == null || p.getId() == null) {
            throw new IllegalArgumentException();
        }
        p = em.merge(p);
        em.remove(p);
    }



    /**
     * Retrieve record of entity Player from persistence as instance, by given Id.
     *
     * @param id
     * @return
     * @throws IllegalArgumentException
     */
    @Override
    public Player find(Long id) {
        if (id == null || id < 0) {
            throw new IllegalArgumentException();
        }
        return em.find(Player.class, id);
    }



    /**
     * Retrieve all records of entity Player from persistence as instances.
     *
     * @return
     */
    @Override
    public List<Player> findAll() {
        Query query = em.createQuery("SELECT a FROM Player a");
        return (List<Player>) query.getResultList();
    }



    /**
     * Checks if all attributes of give instance of entity Player have valid values.
     * This method doesn't checks if Id is defined.
     *
     * @param p
     * @return
     */
    public boolean validate(Player p) {
        return !(
                p == null ||
                p.getFirstName() == null || p.getFirstName().length() == 0 || p.getFirstName().length() > 255 ||
                p.getLastName() == null || p.getLastName().length() == 0 || p.getLastName().length() > 255 ||
                p.getBirthDate() == null ||
                p.getWeight() <= 0 || p.getHeight() <= 0
                );
    }

}
